package com.ruoyi.tianliangedu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tianliangedu.domain.TianliangCourse;
import com.ruoyi.tianliangedu.domain.TianliangFatherSection;
import com.ruoyi.tianliangedu.service.ITianliangCourseService;
import com.ruoyi.tianliangedu.service.ITianliangFatherSectionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 父类章节Controller
 *
 * @author lsj
 * @date 2021-07-12
 */
@Controller
@RequestMapping("/tianliangedu/section")
public class TianliangFatherSectionController extends BaseController {
    private String prefix = "tianliangedu/section";

    @Autowired
    private ITianliangFatherSectionService tianliangFatherSectionService;

    @Autowired
    private ITianliangCourseService tianliangCourseService;

    @RequiresPermissions("tianliangedu:section:view")
    @GetMapping()
    public String section() {
        return prefix + "/section";
    }

    /**
     * 查询父类章节列表
     */
    @RequiresPermissions("tianliangedu:section:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TianliangFatherSection tianliangFatherSection) {
        startPage();
        List<TianliangFatherSection> list = tianliangFatherSectionService.selectTianliangFatherSectionList(tianliangFatherSection);
        return getDataTable(list);
    }

    /**
     * 导出父类章节列表
     */
    @RequiresPermissions("tianliangedu:section:export")
    @Log(title = "父类章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TianliangFatherSection tianliangFatherSection) {
        List<TianliangFatherSection> list = tianliangFatherSectionService.selectTianliangFatherSectionList(tianliangFatherSection);
        ExcelUtil<TianliangFatherSection> util = new ExcelUtil<TianliangFatherSection>(TianliangFatherSection.class);
        return util.exportExcel(list, "父类章节数据");
    }

    /**
     * 新增父类章节
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        TianliangCourse tianliangCourse = new TianliangCourse();
        List<TianliangCourse> list = tianliangCourseService.selectTianliangCourseList(tianliangCourse);
        modelMap.put("listName", list);
        return prefix + "/add";
    }

    /**
     * 新增保存父类章节
     */
    @RequiresPermissions("tianliangedu:section:add")
    @Log(title = "父类章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TianliangFatherSection tianliangFatherSection) {
        return toAjax(tianliangFatherSectionService.insertTianliangFatherSection(tianliangFatherSection));
    }

    /**
     * 修改父类章节
     */
    @GetMapping("/edit/{fatherSectionId}")
    public String edit(@PathVariable("fatherSectionId") Long fatherSectionId, ModelMap mmap) {
        TianliangFatherSection tianliangFatherSection = tianliangFatherSectionService.selectTianliangFatherSectionById(fatherSectionId);
        mmap.put("tianliangFatherSection", tianliangFatherSection);
        return prefix + "/edit";
    }

    /**
     * 修改保存父类章节
     */
    @RequiresPermissions("tianliangedu:section:edit")
    @Log(title = "父类章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TianliangFatherSection tianliangFatherSection) {
        return toAjax(tianliangFatherSectionService.updateTianliangFatherSection(tianliangFatherSection));
    }

    /**
     * 删除父类章节
     */
    @RequiresPermissions("tianliangedu:section:remove")
    @Log(title = "父类章节", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tianliangFatherSectionService.deleteTianliangFatherSectionByIds(ids));
    }
}
